package indi.imooc.datastructure.queue.impl;

import indi.imooc.datastructure.queue.Queue;

/**
 * 循环队列
 * size == 0 队列为空
 * size = capacity = data.length 队列为满
 * @param <E>
 */
public class LoopQueueNoWast<E> implements Queue<E> {

    private E[] data;
    private int front,tail;
    private int size;

    public LoopQueueNoWast(){
        this(10);
    }

    public LoopQueueNoWast(int capatcity){
        data = (E[]) new Object[capatcity];
        front = 0;
        tail = 0;
        size = 0;
    }

    @Override
    public int getSize() {
        return size;
    }

    public int getCapacity(){
        return data.length;
    }

    @Override
    public boolean isEmpty() {
        // 注意，我们不再使用front和tail之间的关系来判断队列是否为空，而直接使用size
        return size == 0;
    }



    @Override
    public void enqueue(E e) {
        if(size == getCapacity()){
            resize(2 * getCapacity());
        }
        data[tail] = e;
        tail = (tail + 1) % data.length;
        size++;
    }

    @Override
    public E dequeue() {
        if(isEmpty()){
            throw new IllegalArgumentException("LoopQueue is empty");
        }
        E e = data[front];
        data[front] = null;
        front = (front + 1 ) % data.length;
        size--;
        if (size == getCapacity() / 4 && getCapacity() / 2 != 0){
            resize(getCapacity() / 2);
        }
        return e;
    }

    @Override
    public E getFront() {
        if(isEmpty()){
            throw new IllegalArgumentException("LoopQueue is empty");
        }
        return data[front];
    }

    /**
     * resize时,数组中元素重新排列,front作为第一个元素
     * @param newCapacity
     */
    private void resize(int newCapacity) {
        E[] newArray = (E[]) new Object[newCapacity];
        // v1.0
        for (int i = 0; i < size ; i++) {
            newArray[i] = data[(i + front) % data.length];
        }

        // v2.0,有bug
//        for (int i = front, index = 0; i != tail ; i = (i + 1) % data.length,index++) {
//            newArray[index++] = data[i];
//        }
        // v2.1
        // front == tail 不再作为队列已满的充要条件
//        for (int i = front, index = 0; index != size ; i = (i + 1) % data.length,index++) {
//            newArray[index] = data[i];
//        }
        data = newArray;
        front = 0;
        tail = size;
    }

    @Override
    public String toString(){
        StringBuilder res = new StringBuilder();
        res.append(String.format("LoopQueue : size = %d , capacity = %d\n", size, getCapacity()));
        res.append("front [ ");
        // v1.0
//        for (int i = front; i != tail; i = (i + 1) % data.length) {
//            res.append(data[i]);
//            if ((i + 1) % data.length != tail){
//                res.append(", ");
//            }
//        }
        // v 1.1
        // front == tail 不再作为队列已满的充要条件
        for (int i = front,index = 0; index != size; i = (i + 1) % data.length) {
            res.append(data[i]);
            index++;
            if (index != size){
                res.append(", ");
            }
        }

        // v2.0
//        for (int i = 0; i < size; i++) {
//            res.append(data[(i + front) % data.length]);
//            if((i + front + 1) % data.length != tail){
////            if(i != size - 1){
//                res.append(", ");
//            }
//        }
        res.append(" ] tail");
        return res.toString();
    }

    public static void main(String[] args) {
        LoopQueueNoWast<Integer> loopQueue = new LoopQueueNoWast<>();
        for (int i = 0; i < 16; i++) {
            System.out.println(i);
            loopQueue.enqueue(i);
            System.out.println(loopQueue.toString());
            if (i % 3 == 2){
                loopQueue.dequeue();
                System.out.println("dequeue -> " + loopQueue.toString());
            }
        }

        System.out.println(loopQueue.toString());
        for (int i = 0; i < 12; i++) {
            loopQueue.dequeue();
            System.out.println(loopQueue.toString());
        }

    }
}
